# -*- coding: utf-8 -*-
"""
utility views for noaccount
"""
##
# @author J.Ducastel <nospam0@ducastel.name>

# imports
from django.http import HttpResponse, HttpResponseRedirect
from django.shortcuts import render_to_response, get_object_or_404
from django.template import RequestContext
from django.core.urlresolvers import reverse
from django.template import TemplateDoesNotExist

from models import User, EmailAccount
from forms import EmailAccountLoginForm, EmailAccountRegisterForm
import api

# exceptions

# classes et fonctions

# Create your views here.
def login_email(request):
    """login processing
    then redirects to POST['redirect_to'] if available"""
    if request.POST:
        # processing
        login_form = EmailAccountLoginForm(request.POST)
        if login_form.is_valid():
            # login user in
            user = login_form.get_account().user
            api.log_user_in(request, user)
            # redirection if requested
            if request.POST.get('redirect_to'):
                return HttpResponseRedirect(request.POST.get('redirect_to'))
    else:
        login_form = EmailAccountLoginForm()
    # rendering project template or failback to own template
    try:
        return render_to_response('login_email.html', RequestContext(request, locals()))
    except TemplateDoesNotExist:
        return render_to_response('noaccount/login_email.html', RequestContext(request, locals()))

def register_email(request):
    """register email account processing
    then redirects to POST['redirect_to'] if available"""
    if request.POST:
        # processing
        register_form = EmailAccountRegisterForm(request.POST)
        if register_form.is_valid():
            user = api.get_or_create_user(request)
            account = register_form.register(user)
            # redirecting if requested
            if request.POST.get('redirect_to'):
                return HttpResponseRedirect(request.POST.get('redirect_to'))
    else:
        register_form = EmailAccountRegisterForm()
    # rendering project template or failback to own template
    try:
        return render_to_response('register_email.html', RequestContext(request, locals()))
    except TemplateDoesNotExist:
        return render_to_response('noaccount/register_email.html', RequestContext(request, locals()))

def logout(request):
    """log out
    then redirects to GET/POST['redirect_to'] if provided"""
    # log out
    del request.session['na_user']
    # redirecting
    if request.GET and request.GET.get('redirect_to'):
        return HttpResponseRedirect(request.GET.get('redirect_to'))
    if request.POST and request.POST.get('redirect_to'):
        return HttpResponseRedirect(request.POST.get('redirect_to'))
    # rendering project template or failback to own template
    try:
        return render_to_response('logout.html', RequestContext(request, locals()))
    except TemplateDoesNotExist:
        return render_to_response('noaccount/logout.html', RequestContext(request, locals()))